package com.macro.mall.service.impl;


import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dao.GuidancePriceDao;
import com.macro.mall.entity.GuidancePrice;
import com.macro.mall.service.GuidancePriceService;

//import com.wuwenze.poi.ExcelKit;
//import com.wuwenze.poi.handler.ExcelReadHandler;
//import com.wuwenze.poi.pojo.ExcelErrorField;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * GuidancePrice的服务接口的实现类
 *
 * @author
 */
@Service
@Slf4j
public class GuidancePriceServiceImpl implements GuidancePriceService {
    private final Logger LOG = LogManager.getLogger(this.getClass());

    @Autowired
    private GuidancePriceDao guidancePriceDao;
    // TODO 当你看到这个方法时你应该创建一个工具类做通用的方法,定义自己的返回格式化
    private static final int C200 = 200;
    private static final int C412 = 412;

    public String resultFormat(int code, Object data) {
        JSONObject result = new JSONObject();
        result.put("code", code);
        if (data != null) {
            result.put("data", data);
        }
        return result.toJSONString();
    }

    @Override
    public List<GuidancePrice> find(String project, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<GuidancePrice> result = guidancePriceDao.selectGuidancePriceByObj(GuidancePrice.builder().project(project).build());

        return result;
    }


    @Override
    public String saveNotNull(GuidancePrice value) {
        if (value == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("执行将GuidancePrice中属性值不为null的数据保存到数据库-->失败:对象不能为空");
            }
            return resultFormat(C412, null);
        }
        int result = guidancePriceDao.insertNotNullGuidancePrice(value);
        if (LOG.isDebugEnabled()) {
            LOG.debug("执行将GuidancePrice中属性值不为null的数据保存到数据库-->结果:", result);
        }
        return resultFormat(C200, result);
    }

    @Override
    public String updateNotNullById(GuidancePrice value) {
        if (value == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("执行通过GuidancePrice的id更新GuidancePrice中属性不为null的数据-->失败:对象为null");
            }
            return resultFormat(C412, null);
        }
        int result = guidancePriceDao.updateNotNullGuidancePriceById(value);
        if (LOG.isDebugEnabled()) {
            LOG.debug("执行通过GuidancePrice的id更新GuidancePrice中属性不为null的数据-->结果:", result);
        }
        return resultFormat(C200, result);
    }

    @Override
    public String deleteById(Integer id) {
        if (id == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("执行通过GuidancePrice的id删除GuidancePrice-->失败:id不能为空");
            }
            return resultFormat(C412, null);
        }
        int result = guidancePriceDao.deleteGuidancePriceById(id);
        if (LOG.isDebugEnabled()) {
            LOG.debug("执行通过GuidancePrice的id删除GuidancePrice-->结果:", result);
        }
        return resultFormat(C200, result);
    }

//    @Override
//    public CommonResult imports(MultipartFile file) throws IOException {
//          List <GuidancePrice> list =new ArrayList<>();
//        List <HashMap> err =new ArrayList<>();
//        ExcelKit.$Import(GuidancePrice.class)
//                .readXlsx(file.getInputStream(), new ExcelReadHandler<GuidancePrice>() {
//
//                    @Override
//                    public void onSuccess(int sheetIndex, int rowIndex, GuidancePrice entity) {
//                        list.add(entity); // 单行读取成功，加入入库队列。
//                    }
//
//                    @Override
//                    public void onError(int sheetIndex, int rowIndex,
//                                        List<ExcelErrorField> errorFields) {
//                        // 读取数据失败，记录了当前行所有失败的数据
//                      HashMap map =  MapUtil.newHashMap();
//                         map.put("sheetIndex", sheetIndex);
//                         map.put("rowIndex", rowIndex);
//                         map.put( "errorFields", errorFields);
//                         err.add(map);
//                        log.info(">>>>>>>>>>>>>");
//                    }
//                });
//                if(err.size()>0){
//                    return CommonResult.error(400,"数据错误");
//                }
//              int result=    guidancePriceDao.insertGuidancePriceByBatch(list);
//            return CommonResult.success(400,"数据错误");
//
//    }


}